#include <Windows.h>
#include <stdint.h>
#include <stdio.h>
#include <comdef.h>

//#include "IHelloWorld.hpp"
//#include "CMyComClass.hpp"
//#include "DllServer.h"
#include "LocalServer_i.c"
//#include "LocalServer_p.c"
#include "LocalServer_h.h"

//#pragma comment(lib, "RpcRT4.lib")

int32_t wmain(int32_t nArgc, const wchar_t *pArgv[])
{
    IHelloWorld* pHelloWorld = nullptr;
    HRESULT hr;

    CoInitialize(NULL);
    
    hr = CoCreateInstance(CLSID_CMyComClass, NULL, CLSCTX_INPROC_SERVER,
        IID_IHelloWorld, (void**)&pHelloWorld);

    if (FAILED(hr)) {
        printf("... failed to create instance (error 0x%08x)\r\n", hr);
        return 0;
    }

    pHelloWorld->HelloWorld(_bstr_t("Hello COM!"));
    pHelloWorld->Release();
    
    /*
    IClassFactory* pICF = NULL;

    hr = CoGetClassObject(CLSID_CMyComClass, CLSCTX_LOCAL_SERVER, NULL, IID_IClassFactory, (void**)&pICF);
    if (!FAILED(hr))
    {
        printf("... successfully obtained local server factory class\r\n");

        //hr = pICF->CreateInstance(NULL, __uuidof(IHelloWorld), (void**)&pHelloWorld);
        hr = pICF->CreateInstance(NULL, IID_IHelloWorld, (void**)&pHelloWorld);
        if (!FAILED(hr))
        {
            printf("... successfully created instance of interface\r\n");
            pHelloWorld->HelloWorld(_bstr_t("Hello COM local EXE server"));
            pHelloWorld->Release();
        }
        else {
            printf("... failed to create instance of IHelloWorld (error 0x%08x)\r\n", hr);
        }
    }
    else {
        printf("... failed to obtain local server factory class (error 0x%08x)\r\n", hr);
    }*/
    
    return 0;
}
